/**
 * Created by zhaojm on 15/3/17.
 */
game.MainLayer = cc.Layer.extend({

    backgroundImgList:null,


    backgroundList : null,

    barrierLayer : null,

    mosterLayer:null,

    coinLayer:null,

    uiLayer : null,

    touchLayer : null,

    isCanMove : null,

    currentTime:null,

    //interval : null,
    speed : null,

    hero:null,

    scoreNum : null,        // 金币数
    distance : null,    // 跑了多少距离
    heart:null, // 血量




    ctor : function(uiLayer){
        this._super();

        this.isDie = false;

        this.heart = game.Config.fishRes.heart;


        this.uiLayer = uiLayer;
        this.scoreNum = 0;
        this.distance = 0;
        this.backgroundList = [];
        this.currentTime = 0;
        this.isCanMove = true;
        //this.interval = game.ConfigHelper.getInterval();
        this.speed = game.ConfigHelper.getSpeed();

        this.backgroundImgList = [res.bg_jpg];


        var size = cc.winSize;



        for(;;){
            var w = this.addOneBackground();
            if( this.backgroundList.length * w >= size.width * 2 && this.backgroundList.length >= 2){
                break;
            }
        }


        this.cloudLayer = new game.CloudLayer();
        this.addChild(this.cloudLayer, 2);


        this.mosterLayer = new game.MosterLayer();
        this.addChild(this.mosterLayer, 2);

        //this.barrierLayer = new game.BarrierLayer();
        //this.addChild(this.barrierLayer, 2);

        this.coinLayer = new game.CoinLayer();
        this.addChild(this.coinLayer, 2);

        this.hero = game.Hero.create();
        this.addChild(this.hero, 2);
        this.hero.setCurrentPosition(cc.p( size.width * game.Config.hero_default_x_percent , size.height * game.Config.hero_default_y_percent));


        this.touchLayer = new game.TouchLayer();
        this.addChild(this.touchLayer, 2);






        if(cc.sys.localStorage.getItem('guide') != '1') {
        //    this.guideLayer = new game.GuideLayer();
        //    this.addChild(this.guideLayer);
        //    this.guideLayer.setSpriteVisible(false);
            //this.guideLayer.setEnabled(false);
            this.scheduleOnce(this.beginGuide, 0.5);// 新手引导第一步
        }

        this.scheduleUpdate();

    },

    beginGuide:function(){
        this.isCanMove = false;
        this.hero.isCanMove = false;
        //this.guideTask =0;
        //// TODO 手 点击可以跳一次
        //this._guideSprite = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('guide0.png'));
        //this._guideSprite.setScale(cc.winSize.width / this._guideSprite.getContentSize().width);
        //this._guideSprite.setPosition(cc.p(cc.winSize.width / 2 - this.getPositionX(), cc.winSize.height / 2));
        ////this._guideSprite.setAnchorPoint(cc.p(0, 0));
        //this.addChild(this._guideSprite, 5);
        //this._guideButton = new cc.MenuItemLabel(new cc.LabelTTF('下一步', 'Arial', 12), this.onGuideButtonCallback);
        //this._guideButton.setAnchorPoint(cc.p(1, 1));
        ////this.addChild(this._guideButton, 5);
        //this._guideButton.setPosition(cc.p(cc.winSize.width - 10 - this.getPositionX(), cc.winSize.height - 10));
        //this._menu = new cc.Menu(this._guideButton);
        //this._menu.setPosition(cc.p(0, 0));
        //this.addChild(this._menu, 5);
        //this.touchLayer.setTouchEnabled(false);
        ////this.guideLayer.setSpriteVisible(true);
        ////this.guideLayer.setSpritePosition(cc.p(cc.winSize.width / 2 - this.getPositionX(), cc.winSize.height / 2));
        ////this.guideLayer.setEnabled(true);
        this.touchLayer.setTouchEnabled(false);
        this.uiLayer.startGuide();

    },





    addOneBackground:function(){
        //cc.log('addOneBackground');

        var bg = null;
        if(this.backgroundList.length == 0){

            bg = this.createBackgroundByIndex(0);

            bg.setPosition(cc.p(0, 0));

        }else{

            var lastBackground = this.backgroundList[this.backgroundList.length - 1];
            var lastIndex = lastBackground.backgroundImgIndex;
            lastIndex = (lastIndex + 1) % this.backgroundImgList.length;
            bg = this.createBackgroundByIndex(lastIndex);
            bg.setPosition(cc.p(lastBackground.getPositionX() + lastBackground.getContentSize().width , 0));
        }
        this.addChild(bg, 1);
        this.backgroundList.push(bg);


        return bg.getContentSize().width;
    },

    createBackgroundByIndex:function(index){


        var bg = new cc.Sprite(this.backgroundImgList[index], cc.rect(2, 2, 720 - 4, 600 - 4)); // 黑线问题

        bg.setAnchorPoint(cc.p(0, 0));
        bg.backgroundImgIndex = index;
        return bg;
    },



    update:function(dt){
        this._super();

        if(!this.isCanMove){
            //this.currentTime = 0;
            return;
        }


        this.setPositionX(this.getPositionX() - this.speed);

        //this.hero.setCurrentPositionX(this.hero.currentPos.x + this.speed);

        // 判断背景删除和创建
        while(true){

            if(this.backgroundList[0].getPositionX() + this.backgroundList[0].getContentSize().width <= cc.director.getVisibleOrigin().x - this.getPositionX()){
                //cc.log('removeBackground');
                //this.removeChild(this.backgroundList[0]);
                var bg = this.backgroundList[0];

                this.backgroundList.splice(0, 1);

                var lastbg = this.backgroundList[this.backgroundList.length - 1];
                //var offset = this.backgroundList[0].getPositionX() + this.backgroundList[0].getContentSize().width - cc.director.getVisibleOrigin().x + this.getPositionX();
                bg.setPositionX(lastbg.getPositionX() + lastbg.getContentSize().width - 2); // 解决黑线问题

                this.backgroundList.push(bg);
                //this.addOneBackground();

            }else{
                break;
            }
        }
        //if(this.backgroundList[this.backgroundList.length - 1].getPositionX() + this.backgroundList[this.backgroundList.length - 1].getContentSize().width  + this.getPositionX() <= cc.winSize.width + 100){
        //    this.addOneBackground();
        //}

    //}

    },

    addOneScore : function(){
        this.scoreNum++;
        this.uiLayer.setScore(this.scoreNum);
    },




});
